package spring.lottery.common.util;

import cn.hutool.captcha.CircleCaptcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import spring.lottery.common.pojo.CaptchaYml;

import java.io.IOException;

/**
 * 验证码工具
 */
@Slf4j
@Configuration
public class CaptchaUtil {

    @Autowired
    CaptchaYml captchaYml;

    public void getCaptcha(HttpServletResponse response) {
        CircleCaptcha captcha = new CircleCaptcha(captchaYml.getWidth(), captchaYml.getHeight(), captchaYml.getConfuse());
        try {
            captcha.write(response.getOutputStream());
            response.setContentType("image/jpeg");
        } catch (IOException e) {
            log.error("写入验证码时发生了错误: {}", e.getMessage(), e);
        }
    }

}
